package com.lsh.airticket.controller;

import com.lsh.airticket.dto.Result;
import com.lsh.airticket.entity.Order;
import com.lsh.airticket.exception.UserException;
import com.lsh.airticket.feign.FlightsFeign;
import com.lsh.airticket.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lsh
 * @date 2021/2/19
 **/
@RestController
@RequestMapping("/orders")
public class OrderController {

    @Autowired
    OrderService orderService;

    @Autowired
    FlightsFeign flightsFeign;

    /**
     * 用户创建 机票的订单
     * @param order
     * @return
     */
    @PostMapping("/order/addOrder")
    public Result addOrder(@RequestBody Order order) {
        int i = orderService.addOrder(order);
        if (i != 1) {
            throw new UserException("支付失败");
        }
        return Result.result().setMsg("支付成功。");
    }

    /**
     * 根据用户 查找该用户的 所有订单。
     * @param userId
     * @return
     */
    @GetMapping("/order/findOrderByUserId")
    public Result findOrderByUserId(Integer userId) {
        List<Order> orderList = orderService.findOrderByUserId(userId);
        return Result.result().addData("orderList", orderList);
    }

    @GetMapping("/findTbTicketById")
    public Result findTbTicketById(Integer ticketId) {
        flightsFeign.findTbTicketById(ticketId);
        return Result.result();
    }
}
